package mo.leetcode_hot;

import java.util.Arrays;

public class L238 {

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] left = new int[n];
        left[0] = 1;
        int[] right = new int[n];
        right[n-1] = 1;
        for (int i = 0; i < n; i++) {
            if (i > 0) {
                left[i] = left[i-1] * nums[i-1];
            }
        }
        for (int i = n-1; i >= 0; i--) {
            if (i > 0) {
                right[i-1] = right[i] * nums[i];
            }
        }
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = left[i] * right[i];
        }
        return res;
    }

    public static void main(String[] args) {
        L238 test = new L238();
        System.out.println(Arrays.toString(test.productExceptSelf(new int[]{1, 2, 3, 4})));
    }
}
